/*
 * @lc app=leetcode.cn id=225 lang=typescript
 *
 * [225] 用队列实现栈
 */

// @lc code=start

// 一个队列实现栈
class MyStack {
    innerList: number[] = [];
    constructor() {}

    push(x: number): void {
        this.innerList.push(x);
    }

    // 主要是使用循环的方式找到最后一个元素，并弹出
    private findTheLast() {
        let count = this.innerList.length - 1;
        let head = this.innerList.shift();
        while (count > 0) {
            this.innerList.push(head!);
            head = this.innerList.shift()!;
            count--;
        }
        return head!;
    }
    pop(): number {
        return this.findTheLast();
    }

    // top 则要把最后一个元素重新写入
    top(): number {
        const last = this.findTheLast();
        this.innerList.push(last);
        return last;
    }

    empty(): boolean {
        return !this.innerList.length;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */
// @lc code=end
